package com.zhaoyang.dao;

import com.zhaoyang.model.Leave;
import com.zhaoyang.model.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵扬
 * @time 2020/4/27 18:43
 */
public class LeaveDao extends BaseDao {

    /**
     * 添加请假信息
     *
     * @param leave 请假信息实体
     * @return 是否添加成功
     */
    public boolean addLeave(Leave leave) {
        String sql = "insert into s_leave values(null,?,?,?,?)";
        int cnt = template.update(sql, leave.getStudentId(), leave.getInfo(),
                Leave.LEAVE_STATUS_WAIT, leave.getRemark());
        return cnt == 1;
    }

    /**
     * 编辑请假菜单
     *
     * @param leave 请假信息
     * @return 是否编辑成功
     */
    public boolean editLeave(Leave leave) {
        String sql = "update s_leave set student_id = ?, status = ?,remark = ? where id = ?";
        int cnt = template.update(sql, leave.getStudentId(),
                leave.getStatus(), leave.getRemark(), leave.getId());
        return cnt == 1;
    }

    /**
     * 删除请假信息
     *
     * @param id 请假信息编号
     * @return 是否删除
     */
    public boolean deleteLeave(int id) {
        String sql = "delete from s_leave where id = ?";
        int cnt = template.update(sql, id);
        return cnt == 1;
    }


    /**
     * 获取分页请假单信息列表
     *
     * @param leave 请假信息
     * @param page  分页信息
     * @return 请假单信息列表
     */
    public List<Leave> getLeaveList(Leave leave, Page page) {
        List<Leave> ret = new ArrayList<Leave>();
        String sql = "select * from s_leave ";
        if (leave.getStudentId() != 0) {
            sql += " and student_id = " + leave.getStudentId() + "";
        }
        sql += " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                Leave l = new Leave();
                l.setId(resultSet.getInt("id"));
                l.setStudentId(resultSet.getInt("student_id"));
                l.setInfo(resultSet.getString("info"));
                l.setStatus(resultSet.getInt("status"));
                l.setRemark(resultSet.getString("remark"));
                ret.add(l);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 获取总记录数
     *
     * @param leave 请假信息
     * @return 总记录数
     */
    public int getLeaveListTotal(Leave leave) {
        int total = 0;
        String sql = "select count(*) total from s_leave ";
        if (leave.getStudentId() != 0) {
            sql += " and student_id = " + leave.getStudentId() + "";
        }
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 查看是否审核通过
     *
     * @param id 请假单编号
     * @return 是否通过
     */
    // TODO >> 遇到的问题: 查询结果是标量 JdbcTemplate该怎么处理
    // TODO >> 自己的解决办法: 根据聚集函数用queryForObject 方法,推断应该亦可以用在查询结果是标量的情况
    public boolean getStatus(int id) {
        String sql = "select status from s_leave where id = ?";
        Integer status = template.queryForObject(sql, Integer.class, id);
        return status.intValue() == Leave.LEAVE_STATUS_AGREE;
    }
}
